<template>
    <div class="back_contain_n">	
        <!-- 头部搜索 -->
        <div class="Search_Top_Part">
            <div class="search_list" >
                <div>
                    <div class="input_flex">
						<el-select clearable v-model="query.deptId" placeholder="部门">
							<el-option v-for="item in deptList" :key="item.id" :label="item.deptName" :value="item.id"></el-option>
						</el-select>
                    </div>
                    <div class="input_flex">
                        <el-input clearable v-model="query.name" placeholder="终端名称"></el-input>
                    </div>
					<div class="input_flex">
						<el-select clearable v-model="query.status" placeholder="访问授权">
							<el-option label="停用" value="1"></el-option>
							<el-option label="启用" value="0"></el-option>
						</el-select>
					</div>
                </div>
            </div>
            <div class="search_bt">
                <span class="zll-search" @click="getList">搜索</span>
                <span class="zll-search-reset" @click="searchReset()">重置</span>
            </div>
        </div>
		<div class="Search_Top_Input">
            <div style="display: flex;">                                              
                <div class="input_flex">
                    <span class="search_left">合计：</span>
                    <span>{{totalCount}}</span>
                </div>
                <div class="input_flex">
                    <span class="search_left">已授权：</span>
                    <span>{{accessibleCount}}</span>
                </div>
                <div class="input_flex">
                    <span class="search_left">未授权：</span>
                    <span>{{unaccessibleCount}}</span>
                </div>
                <div class="addNew">
                    <span @click="edit({},'add')" v-if="hasRole('terminal:extend:add') "><i class="el-icon-circle-plus-outline"></i> 新增</span>
                </div>
            </div>
		</div>
        <!-- table -->
        <sys-table  
            :tableData="tableData" 
            :tableLoading="tableLoading" 
            :tableHeader="tableHeader"
            :scopeWidth="120"
        >
            <template slot-scope="scope" slot="operate">
                <el-button @click="edit(scope.row,'edit')" v-if="hasRole('terminal:extend:edit')" type="text" size="small">编辑</el-button>
                <el-button @click="deleteRow(scope.row)" v-if="hasRole('terminal:extend:del')" type="text" size="small">删除</el-button>
            </template>
        </sys-table>
        <div class="zll-dialog">
            <popout :title="'终端 · ' + (type == 'edit'?'编辑':(type =='see'?'查看':'新增'))" :visible.sync="addDialog" v-if="addDialog">
                <add ref="add" slot="content" :editData="editData" :type="type" :deptList="deptList" @refreshData="refreshData"></add>
                <template slot="bottom">
                    <p class="zll-botton Info" @click="addDialog = false">关 闭</p>
                    <p class="zll-botton" v-if="type !== 'see'" @click="()=>{this.$refs.add.submitForm('addForm')}">提交</p>
                </template>
            </popout>
        </div> 
    </div>
</template>

<script>
import Add from './add'
export default {
    data(){
        return {
            tableLoading:true, //table刷新
            type: '',
            editData: {},
            query: {
				deptId: '',
				name: '',
				status: '',
            },
            tableData: [
                {}
            ],
            tableHeader:[],
            addDialog: false,

			deptList: [],

			totalCount: '',
			accessibleCount: '',
			unaccessibleCount: ''

        }
    },
    methods: {

		getTerminalAllInfo(){
			let self = this;
			self.tableData = [];
			self.tableLoading = true;

			self.totalCount = '';
			self.accessibleCount = '';
			self.unaccessibleCount = '';

			self.$http({
				url: "/jinl/terminal/getTerminalAllInfo",
				method: "post",
				data: JSON.stringify(self.query),
				dataType: 'json',
				contentType: 'application/json',
			}).then(resp=>{
				if (resp.success) {
					self.tableHeader =  [
						{"columnValue": "deptName", "columnName": "使用部门" },
						{"columnValue": "terminalName", "columnName": "终端名称" },
						{"columnValue": "macAddress", "columnName": "Mac地址" },
						{"columnValue": "accessAuthorizationCn", "columnName": "访问授权" },
						{"columnValue": "createrName", "columnName": "创建人" },
					];

					for (let data of resp.result['list']){
						data.createrName = data.createrName+"  "+data.createTimeFt;
					}

					self.tableData = resp.result['list'];
					self.totalCount = resp.result['totalCount'];
					self.accessibleCount = resp.result['accessibleCount'];
					self.unaccessibleCount = resp.result['unaccessibleCount'];
					self.tableLoading = false;
				}
			});

		},

        getList(){//获取表格数据
			let self = this;

			self.tableData = [];
			self.tableLoading = true;
			self.$http({
				url: "/jinl/terminal/getTerminalList",
				method: "post",
				data: JSON.stringify(self.query),
				dataType: 'json',
				contentType: 'application/json',
			}).then(resp=>{
				if (resp.success) {
					self.tableHeader =  [
						{"columnValue": "deptName", "columnName": "使用部门" },
						{"columnValue": "terminalName", "columnName": "终端名称" },
						{"columnValue": "macAddress", "columnName": "Mac地址" },
						{"columnValue": "accessAuthorizationCn", "columnName": "访问授权" },
						{"columnValue": "createrName", "columnName": "创建人" },
					];

					for (let data of resp.result){
						data.createrName = data.createrName+"  "+data.createTimeFt;
					}

					self.tableData = resp.result;
					self.tableLoading = false;
				}
			});
        },

		deleteRow(rowData) {
			let self = this;
			self.$confirm('是否确认删除？', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				self.$http({
					url: '/jinl/terminal/deleteTerminalInfo',
					method: "post",
					data: JSON.stringify({ids: [rowData.id]}),
					dataType: 'json',
					contentType: 'application/json',
				}).then(resp => {
					if (resp.success) {
						self.getTerminalAllInfo();
						self.$message.success('删除成功！');
						return
					}
					self.$message.error(resp.message);
				});
			});
		},

        edit(val,type) {
			let self = this;
			self.addDialog = true;

			self.$nextTick(()=> {
				self.type = type;
				self.editData = Object.assign({},val);
			});
        },

        searchReset() { //重置搜索
            this.query = {
                deptId: '',name: '',status: ''
            };
            this.getList();
        },

		refreshData() {
			this.getTerminalAllInfo();
			this.addDialog = false;
		},


		getDeptList(){//获取部门下拉列表
			let self = this;
			self.$http({
				url: "/jinl/dept/queryDeptChooseList",
				method: "post",
			}).then(resp=>{
				if (resp.success) {
					if (resp.result.length > 0){
						self.deptList = resp.result;
					}
				}
			});
		},
    },
    mounted(){
        let self = this;
		self.getDeptList();
        self.getTerminalAllInfo();
        self.$eventBus.$on("updateWorkList",function () {
            self.addDialog = false;
            self.getTerminalAllInfo();
        })
    },
    components: {
        Add
    },
}
</script>

<style lang="scss" scoped>
@import "@/style/SearchTop.scss";
.input_flex {
    display: flex;align-items: flex-start;width: 150px;
    line-height: 26px;
    .search_left {
        width: 72px;color: #9E9E9E;
        text-align: center;
    }
}
</style>